package com.bwie.customer;

import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Literature;
import com.bwie.common.domain.Message;
import com.bwie.common.domain.User;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.mapper.LiteratureMapper;
import com.bwie.mapper.UserLiteratureMapper;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @className: LiteratureCustomer
 * @Description: TODO
 * @version: v1.８.0
 * @author: 李明
 * @date: 2025/11/10 13:49
 */
@Component
public class LiteratureCustomer {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserLiteratureMapper userLiteratureMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @RabbitListener(queuesToDeclare = @Queue(name = "literature_info"))
    public void literatureCustomer(Literature literature){

        //获取登录人信息
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        User user = (User) redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);

        //创建空对象
        Message message = new Message();
        message.setUserId(user.getId());
        message.setLiteratureId(literature.getId());
        String format = String.format("尊敬的用户{}，你好。数据为{}的值导入失败",
                user.getUsername(), literature.getTitleName());
        message.setContent(format);

        //添加短信信息
        userLiteratureMapper.addMyLiterature(message);
    }

}
